//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package rtk.olinkstar;

import android.util.Log;

public class Buffer {
    private static int mode = 3;

    public Buffer() {
    }

    public static byte[] shortToByteArray(short s) {
        byte[] targets = new byte[2];

        for(int i = 0; i < 2; ++i) {
            int offset = (targets.length - 1 - i) * 8;
            targets[i] = (byte)(s >>> offset & 255);
        }

        return targets;
    }

    public static byte[] longToByteArray(long s) {
        byte[] targets = new byte[8];

        for(int i = 0; i < 8; ++i) {
            int offset = (targets.length - 1 - i) * 8;
            targets[i] = (byte)((int)(s >>> offset & 255L));
        }

        return targets;
    }

    public static byte[] intToByteArray(int s) {
        byte[] targets = new byte[4];

        for(int i = 0; i < 4; ++i) {
            int offset = (targets.length - 1 - i) * 8;
            targets[i] = (byte)(s >>> offset & 255);
        }

        return targets;
    }

    public static void printHexString(byte[] b) {
        String hex = "";

        for(int i = 0; i < b.length; ++i) {
            hex = hex + "0x";
            if((b[i] & 255) < 16) {
                hex = hex + "0";
            }

            hex = hex + Integer.toHexString(b[i] & 255);
            hex = hex + ",";
        }

        Log.v("hex", hex);
    }

    public static int crc16(byte[] buffer) {
        int crc = '\uffff';

        for(int j = 0; j < buffer.length; ++j) {
            crc = (crc >>> 8 | crc << 8) & '\uffff';
            crc ^= buffer[j] & 255;
            crc ^= (crc & 255) >> 4;
            crc ^= crc << 12 & '\uffff';
            crc ^= (crc & 255) << 5 & '\uffff';
        }

        crc &= '\uffff';
        return crc;
    }

    public static byte xor(byte[] buffer) {
        byte cs = 0;

        for(int i = 0; i < buffer.length; ++i) {
            cs ^= buffer[i];
        }

        return cs;
    }

    public static double degreeToDM(double value) {
        double partV = value - (double)((int)value);
        double entierV = value - partV;
        double minuteV = partV * 60.0D;
        double result = entierV * 100.0D + minuteV;
        return result;
    }

    public static double DMToDegree(double value) {
        double value100 = value / 100.0D;
        double partV = value100 - (double)((int)value100);
        double entierV = value100 - partV;
        double minuteV = partV * 100.0D;
        double degreeV = minuteV / 60.0D;
        double result = entierV + degreeV;
        return result;
    }

    public static double[] GaussToBLToGauss(double longitude, double latitude) {
        boolean ProjNo = false;
        double[] output = new double[2];
        double iPI = 0.0174532925199433D;
        byte ZoneWide = 6;
        double a = 6378137.0D;
        double f = 0.0033528106647474805D;
        int ProjNo1 = (int)(longitude / (double)ZoneWide);
        double longitude0 = (double)(ProjNo1 * ZoneWide + ZoneWide / 2);
        longitude0 *= iPI;
        double longitude1 = longitude * iPI;
        double latitude1 = latitude * iPI;
        double e2 = 2.0D * f - f * f;
        double ee = e2 / (1.0D - e2);
        double NN = a / Math.sqrt(1.0D - e2 * Math.sin(latitude1) * Math.sin(latitude1));
        double T = Math.tan(latitude1) * Math.tan(latitude1);
        double C = ee * Math.cos(latitude1) * Math.cos(latitude1);
        double A = (longitude1 - longitude0) * Math.cos(latitude1);
        double M = a * ((1.0D - e2 / 4.0D - 3.0D * e2 * e2 / 64.0D - 5.0D * e2 * e2 * e2 / 256.0D) * latitude1 - (3.0D * e2 / 8.0D + 3.0D * e2 * e2 / 32.0D + 45.0D * e2 * e2 * e2 / 1024.0D) * Math.sin(2.0D * latitude1) + (15.0D * e2 * e2 / 256.0D + 45.0D * e2 * e2 * e2 / 1024.0D) * Math.sin(4.0D * latitude1) - 35.0D * e2 * e2 * e2 / 3072.0D * Math.sin(6.0D * latitude1));
        double xval = NN * (A + (1.0D - T + C) * A * A * A / 6.0D + (5.0D - 18.0D * T + T * T + 14.0D * C - 58.0D * ee) * A * A * A * A * A / 120.0D);
        double yval = M + NN * Math.tan(latitude1) * (A * A / 2.0D + (5.0D - T + 9.0D * C + 4.0D * C * C) * A * A * A * A / 24.0D + (61.0D - 58.0D * T + T * T + 270.0D * C - 330.0D * ee) * A * A * A * A * A * A / 720.0D);
        double X0 = (double)(1000000L * (long)(ProjNo1 + 1) + 500000L);
        double Y0 = 0.0D;
        xval += X0;
        yval += Y0;
        output[0] = xval;
        output[1] = yval;
        return output;
    }

    public static double[] lla_ecef(double lat, double lon, double alt) {
        double lat_rad = lat * 3.141592653589793D / 180.0D;
        double lon_rad = lon * 3.141592653589793D / 180.0D;
        double WGS_MAJOR = 6378137.0D;
        double WGS_E1_SQRT = 0.00669437999014D;
        double WGS_MINOR = 6356752.3142D;
        double e = Math.sqrt(2.723316066819453E11D) / 6378137.0D;
        double N = 6378137.0D / Math.sqrt(1.0D - e * e * Math.sin(lat_rad) * Math.sin(lat_rad));
        double x = (N + alt) * Math.cos(lat_rad) * Math.cos(lon_rad);
        double y = (N + alt) * Math.cos(lat_rad) * Math.sin(lon_rad);
        double z = (N * 0.99330562000986D + alt) * Math.sin(lat_rad);
        double[] result = new double[]{x, y};
        return result;
    }

    public static double approximate(double srcValue, int precis) {
        return (double)((int)(srcValue * Math.pow(10.0D, (double)precis))) / Math.pow(10.0D, (double)precis);
    }
}
